package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityParam;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityParamDao;
import cc.comac.mom.entity.QualityParamEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class QualityParamDataHandler extends ServiceImpl<QualityParamDao, QualityParamEntity> {

	@Autowired
	private QualityParamDao qualityParamDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean insert(QualityParam obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if (obj.getInnerData().getEnable() < 0) {
			throw new RuntimeException("是否启用不能为必填");
		}
		if (obj.getInnerData().getParamType() == null || "".equals(obj.getInnerData().getParamType())) {
			throw new RuntimeException("参数类型不能为必填");
		}
		if (obj.getInnerData().getParamName() == null || "".equals(obj.getInnerData().getParamName())) {
			throw new RuntimeException("参数名称不能为必填");
		}
		if (obj.getInnerData().getParamValue() == null || "".equals(obj.getInnerData().getParamValue())) {
			throw new RuntimeException("参数值不能为必填");
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("param_type", obj.getInnerData().getParamType());
		map.put("param_name", obj.getInnerData().getParamName());
		List<QualityParamEntity> selectByMap = qualityParamDao.selectByMap(map);
		if (selectByMap != null && selectByMap.size() > 0) {
			selectByMap.forEach(item -> {
				if ((StringUtils.isEmpty(item.getParamDetail())&&StringUtils.isEmpty(obj.getInnerData().getParamDetail()))||item.getParamDetail().equals(obj.getInnerData().getParamDetail())) {
					throw new RuntimeException("当前类型"+obj.getInnerData().getParamType()+"下的参数名称"+obj.getInnerData().getParamName()+"下"+obj.getInnerData().getParamDetail()+"已存在！");
				}
			});
		}
		baseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int insert = qualityParamDao.insert(obj.getInnerData());
		if (insert < 1) {
			resultBean.setErrorMessge("参数信息插入失败");
			return resultBean;
		}
		return resultBean;

	}

	public ResultBean update(QualityParam obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			throw new RuntimeException("数据为空");
			// return resultBean;
		}
		if (obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
			// return resultBean;
		}
		if (obj.getInnerData().getEnable() < 0) {
			throw new RuntimeException("是否启用不能为必填");
			// return resultBean;
		}
		if (obj.getInnerData().getParamType() == null || "".equals(obj.getInnerData().getParamType())) {
			throw new RuntimeException("参数类型不能为必填");
			// return resultBean;
		}
		if (obj.getInnerData().getParamName() == null || "".equals(obj.getInnerData().getParamName())) {
			throw new RuntimeException("参数名称不能为必填");
			// return resultBean;
		}
		if (obj.getInnerData().getParamValue() == null || "".equals(obj.getInnerData().getParamValue())) {
			throw new RuntimeException("参数值不能为必填");
			// return resultBean;
		}
		baseEntityDataUtil.updateBaseEntityData(obj.getInnerData());
		int update = qualityParamDao.updateById(obj.getInnerData());
		if (update < 1) {
			throw new RuntimeException("参数信息修改失败");
			// return resultBean;
		}
		return resultBean;

	}

	public ResultBean deleteBatch(List<QualityParam> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			resultBean.setErrorMessge("获取选中数据为0");
			return resultBean;
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for (QualityParam item : list) {
			if (item.getInnerData() == null) {
				resultBean.setErrorMessge("无法获取选中的数据");
				return resultBean;
			}
			listIds.add(item.getInnerData().getId());
		}
		List<QualityParamEntity> selectBatchIds = qualityParamDao.selectBatchIds(listIds);
		if (selectBatchIds.size() > 0) {
			for (QualityParamEntity item : selectBatchIds) {
				baseEntityDataUtil.deleteBaseEntityData(item);
			}
			boolean updateBatch = this.updateBatchById(selectBatchIds);
			if (!updateBatch) {
				resultBean.setErrorMessge("参数信息删除失败");
				return resultBean;
			}
		}
		return resultBean;

	}

	// 通过参数类型和参数名称获取参数值
	public QualityParam getByParamTypeAndName(String paramType, String paramValue) {
		QueryWrapper<QualityParamEntity> queryWrapper = new QueryWrapper<QualityParamEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("param_type", paramType);
		queryWrapper.eq("param_name", paramValue);
		QualityParamEntity selectOne = qualityParamDao.selectOne(queryWrapper);
		if (selectOne == null) {
			return null;
		}
		QualityParam qualityParam = new QualityParam();
		qualityParam.setInnerData(selectOne);
		return qualityParam;
	}

	public LimitPageBean<QualityParamEntity> selectByCondition(QualityParam obj) {
		QueryWrapper<QualityParamEntity> queryWrapper = new QueryWrapper<QualityParamEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getEnable() != -1) {
				queryWrapper.eq("enable", obj.getInnerData().getEnable());
			}
			if (obj.getInnerData().getParamType() != null && !"".equals(obj.getInnerData().getParamType())) {
				queryWrapper.like("param_type", obj.getInnerData().getParamType());
			}
			if (obj.getInnerData().getParamName() != null && !"".equals(obj.getInnerData().getParamName())) {
				queryWrapper.like("param_name", obj.getInnerData().getParamName());
			}
			if (obj.getInnerData().getParamValue() != null && !"".equals(obj.getInnerData().getParamValue())) {
				queryWrapper.like("param_value", obj.getInnerData().getParamValue());
			}

		}
		Page<QualityParamEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<QualityParamEntity> iPage = qualityParamDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<QualityParamEntity> limitPageBean = (LimitPageBean<QualityParamEntity>) LimitPageBean.of(iPage);
		return limitPageBean;

	}

	public LimitPageBean<QualityParamEntity> selectAllByCondition(QualityParam obj){
		QueryWrapper<QualityParamEntity> queryWrapper = new QueryWrapper<QualityParamEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getEnable() != -1) {
				queryWrapper.eq("enable", obj.getInnerData().getEnable());
			}
			if (obj.getInnerData().getParamType() != null && !"".equals(obj.getInnerData().getParamType())) {
				queryWrapper.like("param_type", obj.getInnerData().getParamType());
			}
			if (obj.getInnerData().getParamName() != null && !"".equals(obj.getInnerData().getParamName())) {
				queryWrapper.like("param_name", obj.getInnerData().getParamName());
			}
			if (obj.getInnerData().getParamValue() != null && !"".equals(obj.getInnerData().getParamValue())) {
				queryWrapper.like("param_value", obj.getInnerData().getParamValue());
			}

		}
		List<QualityParamEntity> paramEntityList = qualityParamDao.selectList(queryWrapper);
		LimitPageBean<QualityParamEntity> limitPageBean = new LimitPageBean<QualityParamEntity>();
		limitPageBean.setEntityList(paramEntityList);
		return limitPageBean;
	}

}
